


package action;

//import java.util.ArrayList;

import java.sql.Timestamp;
import java.util.Map;

import java.util.*;

import models.*;

import models.IssueData;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;




public class UpdateIssueAction extends ActionSupport {
	Map<String, Object> session;
	private static final long serialVersionUID = 1L;
	private String projectName;
	private String category;
	private String subject;
	private String fulldescription;
	private String tags;
	private String emailid;
	private String phoneno;
	private String SecQues;
	private String answer;
	private int issue_num;

	public int getIssue_num() {
		return issue_num;
	}
	public void setIssue_num(int issue_num) {
		this.issue_num = issue_num;
	}
	public String getProjectName() {
		return projectName;
	}
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String getSubject() {
		return subject;
	}
	public void setSubject(String subject) {
		this.subject = subject;
	}
	public String getFulldescription() {
		return fulldescription;
	}
	public void setFulldescription(String fulldescription) {
		this.fulldescription = fulldescription;
	}
	public String getTags() {
		return tags;
	}
	public void setTags(String tags) {
		this.tags = tags;
	}
	public String getEmailid() {
		return emailid;
	}
	public void setEmailid(String emailid) {
		this.emailid = emailid;
	}
	public String getPhoneno() {
		return phoneno;
	}
	public void setPhoneno(String phoneno) {
		this.phoneno = phoneno;
	}
	public String getSecQues() {
		return SecQues;
	}
	public void setSecQues(String secQues) {
		SecQues = secQues;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
//	private ArrayList<DeveloperData> Developers = new ArrayList<DeveloperData>();
	public String execute() {
		System.out.println("-------------------in update issue---------------");
		
		
		Date date = new Date();
		Timestamp timestamp = new Timestamp(date.getTime());
		System.out.println("timestamp is"+timestamp);
		if(this.emailid.length()==0)
		{
			addActionError(getText("email id cannot be empty"));
			return "error" ;
		}
		
		  String emailPattern="^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})";
		  if(false==this.emailid.matches(emailPattern))
		  {
			  addActionError(getText("email format wrong"));
				return "error" ;
		  } 

		  if (this.phoneno.contains("[a-zA-Z]+") || this.phoneno.length() < 10){
			  addActionError(getText("please enter a valid mobile number.It should not contain alphabet and length should be 10"));
				return "error" ;
		  }
		
		
		
		if(this.phoneno.length()==0)
		{
			addActionError(getText("phone no. cannot be empty"));
			return "error" ;
		}
		if(this.answer.length()==0)
		{
			addActionError(getText("please type an answer"));
			return "error" ;
		}
		
		
		
		IssueData Issue = new IssueData();
		//System.out.println("this is the issue detl"+category+""+emailid+""+phoneno+""+fulldescription+""+subject+","+);
		session = ActionContext.getContext().getSession();
		IssueData issue = (IssueData)session.get("Issue");
		//System.out.println("issue nnr iss sa       "+issue.getIssue_num());
		int iss_from_session = issue.getIssue_num();
		Issue.setCategory(this.category);
		Issue.setClient_email(this.emailid);
		Issue.setClient_phone_no(this.phoneno);
		Issue.setDescp(this.fulldescription);
		Issue.setIssue_subject(this.subject);
		Issue.setAssignedto("");
		Issue.setSeq_qn(this.SecQues);
		Issue.setSeq_ans(this.answer);
		Issue.setIssue_subject(this.subject);
		java.util.Date dt = new java.util.Date();
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentTime = sdf.format(dt);
		System.out.println("the tims is"+currentTime);
		Issue.setCreated_datetime(currentTime);
		//double a = Math.random();
		//System.out.println(a);
		//a= a*100000;
		//int b= (int)Math.round(a);
	//	Issue.setIssue_num(b);
		
		Issue.updateIssue(iss_from_session);
		
		System.out.println("ajkhsa");
		TagData tags_insert = new TagData();
		//System.out.println("issue nbmask is " +issue_num);
		IssueData Issuefind=IssueData.findOne("where issue_num= "+iss_from_session);
		int iss_id_from_session = issue.getIssue_id();
		//System.out.println("ajkhsa");
		int issue_id = Issuefind.getIssue_id();
		
		StringTokenizer st = new StringTokenizer(tags, ",");
	
		tags_insert.delete(iss_id_from_session);
		while(st.hasMoreTokens()) {
			String key = st.nextToken();
		//	System.out.println(key + "\t"+issue_id );
			tags_insert.setIssueId(issue_id);
			tags_insert.setTagName(key);
			tags_insert.insert();
			
		
			} 
		//System.out.println("io,m here");
		return "success";
	}
	
}
